using UnityEngine;
using System.Collections;

public class AchieveListItem : MonoBehaviour {
	
	public TextMesh title;
	public TextMesh description;
	
	public bool unlocked;
	public int id;
	
	public bool scroolMode = false;
	public Vector2 lastMousePosition;
	public float magnitudeDeRolagem = 1;
	private float cronometro;
	public float tempoMaximoParaClick = 1;
	
	
	public void SetId(int id)
	{
		this.id = id;
	}
	
	void Update () {
		
		if(scroolMode)
		{
			cronometro += Time.deltaTime;
			
			float diferencaY =  lastMousePosition.y - Input.mousePosition.y ;
			lastMousePosition = Input.mousePosition;
			Debug.Log(diferencaY + "	 diferenca Y");
			Camera.mainCamera.transform.position = new Vector3( Camera.mainCamera.transform.position.x, Camera.mainCamera.transform.position.y + magnitudeDeRolagem /100* diferencaY, Camera.mainCamera.transform.position.z);
			
			if(Camera.mainCamera.transform.position.y > ScrollPlane.yMax)
			{
				Camera.mainCamera.transform.position = new Vector3( Camera.mainCamera.transform.position.x, ScrollPlane.yMax, Camera.mainCamera.transform.position.z);
			}
			
			if(Camera.mainCamera.transform.position.y < ScrollPlane.yMin)
			{
				Camera.mainCamera.transform.position = new Vector3( Camera.mainCamera.transform.position.x, ScrollPlane.yMin, Camera.mainCamera.transform.position.z);
			}
		
		}
	}
	
	void OnMouseDown()
	{
		scroolMode = true;
		this.lastMousePosition = Input.mousePosition;
		Debug.Log("Scroll mode true");
	}
	void OnMouseUp()
	{
	
		scroolMode = false;
		if(cronometro <= tempoMaximoParaClick){
			AchievementManager.getInstance.Open(id);
			Debug.Log("Clicou no achieveListItem");
			cronometro = 0;
		}
	}
	
	void SetText(string text)
	{	
		TextMesh tm = GetComponent<TextMesh>();
		tm.text = text;
	}
	
	public void changeColor(bool selected)
	{
		if(selected)
		{
			if(!description.gameObject.GetComponent<SpecialCube>())
				description.gameObject.AddComponent("SpecialCube");
			
			if(!title.gameObject.GetComponent<SpecialCube>())
				title.gameObject.AddComponent("SpecialCube");
			
		}else if(unlocked)
		{	
			if(description.gameObject.GetComponent<SpecialCube>())
				Destroy(description.gameObject.GetComponent<SpecialCube>());
			
			if(title.gameObject.GetComponent<SpecialCube>())
				Destroy(title.gameObject.GetComponent<SpecialCube>());
			
			title.renderer.material.color = Color.white;
			description.renderer.material.color = Color.white;
		}else
		{
			if(description.gameObject.GetComponent<SpecialCube>())
				Destroy(description.gameObject.GetComponent<SpecialCube>());
			
			if(title.gameObject.GetComponent<SpecialCube>())
				Destroy(title.gameObject.GetComponent<SpecialCube>());
			
			title.renderer.material.color = Color.grey;
			description.renderer.material.color = Color.grey;
		}
	}
	
	public void activeGetText()
	{
		title.GetComponent<UI_TextMessage>().enabled = true;
		description.GetComponent<UI_TextMessage>().enabled = true;
	}
	
	public void SetUnlocked(bool unlocked)
	{
		if(unlocked)
		{
			if(!description.gameObject.GetComponent<SpecialCube>())
				description.gameObject.AddComponent("SpecialCube");
			
			if(!title.gameObject.GetComponent<SpecialCube>())
				title.gameObject.AddComponent("SpecialCube");
		}
		else
		{
			description.GetComponent<UI_TextMessage>().enabled = false;
			description.text = "***********";
			title.renderer.material.color = Color.grey;
			description.renderer.material.color = Color.grey;
		}
		
		this.unlocked = unlocked;
	}
}
